/**
 * 分页
 * @param currentPage      当前页
 * @param totalPage        总页数
 * @param recordPerPage    每页记录数,默认值：10
 * @param totalRecord      总记录数
 * @param turnToPath       分页跳转路径,比如："${req.contextPath}/mtl/initialMedicineTypeApproveListUI.action"
 * @param queryString      注意按以下格式: "&key1=value1&key2=value2"
 * Added by: 侯彦涛
 */
function Pagination(currentPage, totalPage, recordPerPage, totalRecord, turnToPath, queryString) {
    this.currentPage = currentPage;             //当前页数
    this.totalPage = totalPage;                 //总页数
    this.recordPerPage = recordPerPage;         //每页记录数
    this.totalRecord = totalRecord;             //总记录数
    this.initPagination = initPagination;
    this.initPaginationForm = initPaginationForm;//用于表单形式的提交
    this.pageJumpExce = pageJumpExce;//页面跳转程序的执行
    this.turnToPath = turnToPath;                //转向路径
    this.queryString = queryString;              //查询字符串
    this.isform = false;                        //判定是否用于表单形式的提交
    this.iForm;                                 //提交时所用的表单
    this.first = first;
    this.pre = pre;
    this.next = next;
    this.last = last;
    this.turnToPage = turnToPage;
    this._currentFormPageReset = false;//用表单提交时，是否在提交前进行页面刷新，默认为否(false)
}
//初始化分页JS对象到页面,在页面DIV写入HTML代码
function initPagination(paginationContainer,showPageCount) {
    if(!paginationContainer)paginationContainer="pagination";
    if(!showPageCount)showPageCount=10;
    if(showPageCount<5)showPageCount=5;
    var setTotalPage;
    var setCurrentPage;
    var setNext;
    if (this.totalPage == 0) {
        setTotalPage = "disabled";
        setNext = "disabled";
    }
    if (this.currentPage == 1) {
        setCurrentPage = "disabled";
    }
    if (this.currentPage == this.totalPage) {
        setNext = "disabled";
    }
    if (this.totalPage == 1) {
        setTotalPage = "disabled";
    }
    var paginationDiv = document.getElementById(paginationContainer);
    if(true){
    var pageInName="page-"+paginationContainer;
    var tpl="";
    tpl+="";
    tpl+="<div class=\"pages\">";
    tpl+="${pageList}";
    tpl+="<span class=\"pagesone\">共<font color=\"red\">${totalRecord}<\/font>条|页数:${currentPage}\/${totalPage}<\/span>";
    tpl+="<div class=\"fl\">到第<\/div><input id=\""+pageInName+"\" name=\""+pageInName+"\" type=\"text\" size=\"3\" onkeydown=\"if(event.keyCode==13){pagination.turnToPage(document.getElementById('"+pageInName+"').value);return false;}\"><div class=\"fl\">页<\/div><button onclick=\"pagination.turnToPage(document.getElementById('"+pageInName+"').value);return false;\">确认<\/button>";
    tpl+="<\/div>";
    var showPages="";

    var currentPage=parseInt(this.currentPage);
    var totalPage=parseInt(this.totalPage);
    if(totalPage<=showPageCount){
        for(var i=1;i<=this.totalPage;i++){
            if(i==currentPage){showPages+="<b>"+(currentPage)+"</b>";continue}
            showPages+="<a href=\"javascript:turnToPage('"+(i)+"')\">"+i+"<\/a>"
        }
    }else{
        var pre=0;
        var next=0;
        pre=currentPage-3;
        pre=pre>0?pre:1;
        next=currentPage+(showPageCount-4);
        
        next=next<showPageCount?showPageCount:(next>totalPage?totalPage:next);
        pre=(next-pre+1<showPageCount)?(next-showPageCount+1):pre;
        if(pre>=2){
            showPages+="<a href=\"javascript:turnToPage('1')\">1...<\/a>"
        }

        for(var i=pre;i<=next;i++){
            if(i==this.currentPage){showPages+="<b>"+(currentPage)+"</b>";continue}
            showPages+="<a href=\"javascript:turnToPage('"+(i)+"')\">"+i+"<\/a>"
        }
        if(this.totalPage-next>0){
         showPages+="<a href=\"javascript:turnToPage('"+(next+1)+"')\">下"+showPageCount+"页<\/a>";
        }
    }

    tpl=tpl.replace("${pageList}",showPages);
    tpl=tpl.replace("${totalRecord}",this.totalRecord);
    tpl=tpl.replace("${currentPage}",this.currentPage);
    tpl=tpl.replace("${totalPage}",this.totalPage);
    paginationDiv.innerHTML = tpl;
    }else{
    var baseStr = "<table align='center' class='table-page' style='left:20px;'>" + "<tr><td><span id='showTotal'>共 " + this.totalRecord + " 条记录&nbsp;每页 " + this.recordPerPage
            + " 条&nbsp;共 " + this.totalPage + " 页&nbsp;当前第 " + this.currentPage + " 页&nbsp;转到第<input type='text' name='page' size='1' onkeydown='if(event.keyCode==13){pagination.turnToPage();return false;}'>页<input type='button' name='go' value='GO' class='inputButton' style='padding-top: 4px;' onclick='pagination.turnToPage();' " + setTotalPage + "></span>";
    var firstButtonStr = "<input type='button' name='firstPage' style='padding-top: 3px;' value='首页' class='inputButton' onclick='pagination.first();' " + setCurrentPage + ">";
    var preButtonStr = "<input type='button' name='prePage' style='padding-top: 4px;' value='上页' class='inputButton' onclick='pagination.pre();' " + setCurrentPage + ">";
    var nextButtonStr = "<input type='button' name='nextPage' style='padding-top: 4px;' value='下页' onclick='pagination.next();' class='inputButton' " + setNext + ">";
    var lastButtonStr = "<input type='button' name='lastPage' style='padding-top: 4px;' value='尾页' class='inputButton' onclick='pagination.last();'" + setNext + ">";
    var buttomStr = "</td></tr></table>";
    paginationDiv.innerHTML = baseStr + firstButtonStr + preButtonStr + nextButtonStr + lastButtonStr + buttomStr;
    }
}
//如果页面用FORM提交,可用此初始化函数而不直接用initPagination
function initPaginationForm(vForm){
    this.iForm = vForm;
    this.isform = true;
    this.initPagination();
}
//转到首页函数
function first() {
    var jump;
    var spsing = "";
    if(this.turnToPath.indexOf("?")==-1) spsing="?"; else spsing="&";
    if (typeof(this.queryString) != "undefined") {
        jump = this.turnToPath + spsing+"currentPage=1&recordPerPage=" + recordPerPage +"&"+ this.queryString+"&queryString="+ escape(this.queryString);
    } else {
        jump = this.turnToPath + spsing+"currentPage=1&recordPerPage=" + recordPerPage;
    }
    this.pageJumpExce(jump);
}
//上一页
function pre() {
    var jump;
    var spsing = "";
    if(this.turnToPath.indexOf("?")==-1) spsing="?"; else spsing="&";
    if (typeof(this.queryString) != "undefined") {
        jump = this.turnToPath + spsing + "currentPage=" + (currentPage - 1) + "&recordPerPage=" + recordPerPage +"&"+ this.queryString+"&queryString="+ escape(this.queryString);
    } else {
        jump = this.turnToPath + spsing + "currentPage=" + (currentPage - 1) + "&recordPerPage=" + recordPerPage;
    }
    this.pageJumpExce(jump);
}
//下一页
function next() {
    var jump;
    var spsing = "";
    if(this.turnToPath.indexOf("?")==-1) spsing="?"; else spsing="&";
    if (typeof(this.queryString) != "undefined") {
        jump = this.turnToPath + spsing+"currentPage=" + (Number(currentPage) + 1) + "&recordPerPage=" + recordPerPage +"&"+ this.queryString+"&queryString="+ escape(this.queryString);
    } else {
        jump = this.turnToPath + spsing+"currentPage=" + (Number(currentPage) + 1) + "&recordPerPage=" + recordPerPage;
    }
    this.pageJumpExce(jump);
}
//尾页
function last() {
    var jump;
    var spsing = "";
    if(this.turnToPath.indexOf("?")==-1) spsing="?"; else spsing="&";
    if (typeof(this.queryString) != "undefined") {
        jump = this.turnToPath + spsing+"currentPage=" + totalPage + "&recordPerPage=" + recordPerPage +"&"+ this.queryString+"&queryString="+ escape(this.queryString);
    } else {
        jump = this.turnToPath + spsing+"currentPage=" + totalPage + "&recordPerPage=" + recordPerPage;
    }
    this.pageJumpExce(jump);
}
//GO按钮事件,转向某一页
function turnToPage(p) {
    var pageCount=p;
    if(!pageCount)pageCount=document.all.page.value;
    var jump;
    if(pageCount==""){
       alert("请输入一个不大于总页数的整数!");
        document.all.page.focus();
        return;
    }else if(Number(pageCount)>Number(this.totalPage)){
       alert("转到页面数值大于总页数!");
        document.all.page.focus();
        return;
    }else if(Number(pageCount)<=0){
       alert("请输入一个大于零的整数!");
       document.all.page.focus();
       return;
    }else if(!checkPositiveInteger(pageCount)){
       document.all.page.focus();
       return;
    }
    var spsing = "";
    if(this.turnToPath.indexOf("?")==-1) spsing="?"; else spsing="&";
    if (typeof(this.queryString) != "undefined") {
        //jump = this.turnToPath + spsing+"currentPage=" + pageCount + "&recordPerPage=" + recordPerPage +"&"+ this.queryString+"&queryString="+ escape(this.queryString);
        jump = this.turnToPath + spsing+"currentPage=" + pageCount +"&"+ this.queryString+"&queryString="+ escape(this.queryString);
    } else {
        jump = this.turnToPath + spsing+"currentPage=" + pageCount + "&recordPerPage=" + recordPerPage;
    }
    this.pageJumpExce(jump);
}
//如是FORM提交,用此函数转向分页
function pageJumpExce(jumpStr)
{
    if(this.isform){
       this.iForm.action = jumpStr;
       if(this._currentFormPageReset) this.iForm.reset();
       this.iForm.submit();
    }else{
      window.location.href = jumpStr;
    }
}
//转到页数,正整数验证
function checkPositiveInteger(str) {
    var reg = /^[0-9]*[1-9][0-9]*$/;
    if (arr = str.match(reg)) {
        if (!(str.indexOf("0") == 0)) {
            return true;
        } else
        {
            alert("转到页面数值不能以零开头！");
            return false;
        }
    }
    else
    {
        alert("转到页面数值不是正整数");
        return false;
    }
}
//为分页控制按钮添加onkeydown事件
/*******************************************
*ctrl+End:转到尾页                         *
*ctrl+Home:转到首页                        *
*ctrl+PageDown或+:下一页                   *
*ctrl+PageUp或-:上一页                     *
*大,小键盘数字键:转到页数                  *
*******************************************/
window.attachEvent("onload",function(){document.body.attachEvent("onkeydown",keyEventForPage)});
function keyEventForPage(){
  if(event.shiftKey && event.keyCode==35){//END键
      if(document.all.lastPage) document.all.lastPage.click();
  }else if(event.shiftKey && event.keyCode==36){//HOME键
      if(document.all.firstPage) document.all.firstPage.click();
  }else if((event.shiftKey && event.keyCode==33)){//PageUp,-键
     if(document.all.prePage) document.all.prePage.click();
  }else if((event.shiftKey && event.keyCode==34)){//PageDown,+键
     if(document.all.nextPage) document.all.nextPage.click();
  }/*else if(Number(event.keyCode)>=96 && Number(event.keyCode)<=105){
    document.all.page.focus();
  }else if( (Number(event.keyCode)>=48 && Number(event.keyCode)<=57)){
    document.all.page.focus();
  }*/
}
